﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Zhuang.Utility.Common.Validation
{
    public class Validator
    {
        public static bool IsStringDate(string _value)
        {
            bool result;
            try
            {
                DateTime dt = DateTime.Parse(_value);
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
                result = false;
                return result;
            }
            result = true;
            return result;
        }
        public static bool IsNumberId(string _value)
        {
            return Validator.QuickValidate("^[1-9]*[0-9]*$", _value);
        }
        public static bool IsLetterOrNumber(string _value)
        {
            return Validator.QuickValidate("^[a-zA-Z0-9_]*$", _value);
        }
        public static bool IsNumber(string _value)
        {
            return Validator.QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$", _value);
        }
        public static bool QuickValidate(string _express, string _value)
        {
            Regex myRegex = new Regex(_express);
            return _value.Length != 0 && myRegex.IsMatch(_value);
        }
        public static bool TelCheck(string M)
        {
            bool result;
            try
            {
                long.Parse(M);
            }
            catch
            {
                result = false;
                return result;
            }
            RegexOptions options = RegexOptions.Multiline;
            Regex regex = new Regex("13\\d{9}|159\\d{8}|01\\d{9}|02\\d{9}|0[^1-2]\\d{9}|0[^1-2]\\d{10}", options);
            bool isMatch = regex.IsMatch(M);
            result = isMatch;
            return result;
        }
        public static string StringTryConvert(DBNull str, string Result)
        {
            return Result;
        }
        public static string StringTryConvert(string str, string Result)
        {
            string result;
            if (str != null && str != "")
            {
                result = str;
            }
            else
            {
                result = Result;
            }
            return result;
        }
        public static string SqlStrCheck(string SqlStr)
        {
            return SqlStr.Replace("'", "‘");
        }
        public static string Str_GetGUID()
        {
            return ((uint)Guid.NewGuid().GetHashCode()).ToString();
        }
    }
}
